﻿using Chloe.Infrastructure.Interception;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ChloeEx.Core
{
    public class DbCommandInterceptor : IDbCommandInterceptor
    {
        ICommandPrinter printer;
        public DbCommandInterceptor(ICommandPrinter _printer)
        {
            printer = _printer;
        }
        /* 执行 DbCommand.ExecuteReader() 时调用 */
        public void ReaderExecuting(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
        {

            interceptionContext.DataBag.Add("startTime", DateTime.Now);
            //Console.WriteLine(command.CommandText);
            //Console.WriteLine(JsonConvert.SerializeObject(command.Parameters));
            printer.WriteSqlString(command, "ReaderExecuting");
        }
        /* 执行 DbCommand.ExecuteReader() 后调用 */
        public void ReaderExecuted(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
        {
            //DateTime startTime = (DateTime)(interceptionContext.DataBag["startTime"]);
            //Console.WriteLine(DateTime.Now.Subtract(startTime).TotalMilliseconds);
            //if (interceptionContext.Exception == null)
            //    Console.WriteLine(interceptionContext.Result.FieldCount);
            printer.WriteSqlString(command, "ReaderExecuted");
        }

        /* 执行 DbCommand.ExecuteNonQuery() 时调用 */
        public void NonQueryExecuting(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            //interceptionContext.DataBag.Add("startTime", DateTime.Now);
            //Console.WriteLine(command.CommandText);
            //Console.WriteLine(JsonConvert.SerializeObject(command.Parameters));
            printer.WriteSqlString(command, "NonQueryExecuting");
        }
        /* 执行 DbCommand.ExecuteNonQuery() 后调用 */
        public void NonQueryExecuted(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            //DateTime startTime = (DateTime)(interceptionContext.DataBag["startTime"]);
            //Console.WriteLine(DateTime.Now.Subtract(startTime).TotalMilliseconds);
            //if (interceptionContext.Exception == null)
            //    Console.WriteLine(interceptionContext.Result);
            //Console.WriteLine(JsonConvert.SerializeObject(command.Parameters));
            printer.WriteSqlString(command, "NonQueryExecuted");
        }

        /* 执行 DbCommand.ExecuteScalar() 时调用 */
        public void ScalarExecuting(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            //interceptionContext.DataBag.Add("startTime", DateTime.Now);
            //Console.WriteLine(command.CommandText);
            //Console.WriteLine(JsonConvert.SerializeObject(command.Parameters));
            printer.WriteSqlString(command, "ScalarExecuting");
        }
        /* 执行 DbCommand.ExecuteScalar() 后调用 */
        public void ScalarExecuted(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            //DateTime startTime = (DateTime)(interceptionContext.DataBag["startTime"]);
            //Console.WriteLine(DateTime.Now.Subtract(startTime).TotalMilliseconds);
            //if (interceptionContext.Exception == null)
            //    Console.WriteLine(interceptionContext.Result);
            //Console.WriteLine(JsonConvert.SerializeObject(command.Parameters));
            printer.WriteSqlString(command, "ScalarExecuted");
        }
    }
}
